import request from '@/utils/request';
import { requestWrapper, randomDate, randomInt, randomPick } from '@/utils/mock';

// 获取菜单列表
export function getMenuList(params) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/system/menu/list',
      method: 'get',
      params
    }),
    // 模拟数据
    () => {
      // 创建一些父级菜单
      const parentMenus = Array.from({ length: 5 }, (_, index) => ({
        menuId: index + 1,
        menuName: `菜单${index + 1}`,
        parentId: 0,
        orderNum: index + 1,
        path: `/menu${index + 1}`,
        component: 'Layout',
        menuType: 'M', // 目录
        status: '0',
        icon: `system${index + 1}`,
        children: []
      }));

      // 为每个父级菜单创建子菜单
      parentMenus.forEach((parent, parentIndex) => {
        const subMenuCount = randomInt(2, 5);
        const subMenus = Array.from({ length: subMenuCount }, (_, index) => {
          const menuId = (parentIndex + 1) * 10 + index + 1;
          return {
            menuId,
            menuName: `子菜单${menuId}`,
            parentId: parent.menuId,
            orderNum: index + 1,
            path: `subMenu${index + 1}`,
            component: `system/menu${parent.menuId}/subMenu${index + 1}`,
            menuType: 'C', // 菜单
            status: '0',
            icon: `menu${index + 1}`,
            children: []
          };
        });
        parent.children = subMenus;
      });

      // 处理搜索条件
      let filteredMenus = [...parentMenus];

      if (params) {
        if (params.menuName) {
          // 实现带有子菜单匹配的搜索
          filteredMenus = filteredMenus.filter(parent => {
            // 父菜单名称匹配
            if (parent.menuName.includes(params.menuName)) {
              return true;
            }
            
            // 子菜单名称匹配，保留匹配的子菜单
            const matchedChildren = parent.children.filter(child => 
              child.menuName.includes(params.menuName)
            );
            
            if (matchedChildren.length > 0) {
              parent.children = matchedChildren;
              return true;
            }
            
            return false;
          });
        }
        
        if (params.status !== undefined && params.status !== '') {
          filteredMenus = filteredMenus.filter(parent => {
            if (parent.status !== params.status) {
              return false;
            }
            
            // 过滤子菜单
            parent.children = parent.children.filter(child => {
              return child.status === params.status;
            });
            
            return true;
          });
        }
      }

      return filteredMenus;
    }
  );
}

// 获取菜单树形结构
export function getMenuTree() {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/system/menu/tree',
      method: 'get'
    }),
    // 模拟数据
    [
      {
        id: 1,
        label: '系统管理',
        children: [
          { id: 100, label: '用户管理' },
          { id: 101, label: '角色管理' }
        ]
      },
      {
        id: 2,
        label: '系统监控',
        children: [
          { id: 200, label: '在线用户' },
          { id: 201, label: '服务监控' }
        ]
      }
    ]
  );
}

// 获取菜单详情
export function getMenuDetail(menuId) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: `/system/menu/${menuId}`,
      method: 'get'
    }),
    // 模拟数据
    {
      menuId,
      menuName: `菜单${menuId}`,
      parentId: menuId > 10 ? Math.floor(menuId / 10) : 0,
      orderNum: menuId % 10 || 1,
      path: menuId > 10 ? `menu${menuId}` : `/menu${menuId}`,
      component: menuId > 10 ? `system/menu${menuId}` : 'Layout',
      menuType: menuId > 100 ? 'F' : (menuId > 10 ? 'C' : 'M'),
      status: '0',
      perms: menuId > 10 ? `system:menu${menuId}:list` : '',
      icon: `icon${menuId}`,
      visible: '0',
      isCache: '0',
      isFrame: '1'
    }
  );
}

// 新增菜单
export function addMenu(data) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/system/menu',
      method: 'post',
      data
    }),
    // 模拟数据
    null
  );
}

// 修改菜单
export function updateMenu(data) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/system/menu',
      method: 'put',
      data
    }),
    // 模拟数据
    null
  );
}

// 删除菜单
export function deleteMenu(menuId) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: `/system/menu/${menuId}`,
      method: 'delete'
    }),
    // 模拟数据
    null
  );
}

// 获取用户菜单权限列表
export function getUserMenus() {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/system/menu/user',
      method: 'get'
    }),
    // 模拟数据
    {
      menus: [
        {
          name: '系统管理',
          path: '/system',
          component: 'Layout',
          meta: {
            title: '系统管理',
            icon: 'system',
            isKeepAlive: true
          },
          children: [
            {
              name: '用户管理',
              path: 'user',
              component: 'system/user/index',
              meta: {
                title: '用户管理',
                icon: 'user',
                isKeepAlive: true
              }
            },
            {
              name: '角色管理',
              path: 'role',
              component: 'system/role/index',
              meta: {
                title: '角色管理',
                icon: 'peoples',
                isKeepAlive: true
              }
            }
          ]
        },
        {
          name: '系统监控',
          path: '/monitor',
          component: 'Layout',
          meta: {
            title: '系统监控',
            icon: 'monitor',
            isKeepAlive: true
          },
          children: [
            {
              name: '在线用户',
              path: 'online',
              component: 'monitor/online/index',
              meta: {
                title: '在线用户',
                icon: 'online',
                isKeepAlive: true
              }
            }
          ]
        }
      ],
      permissions: ['system:user:list', 'system:user:add', 'system:user:edit', 'system:user:delete', 'system:role:list']
    }
  );
}

// 更新菜单状态
export function updateMenuStatus(id, status) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: `/system/menu/${id}/status/${status}`,
      method: 'patch'
    }),
    // 模拟数据
    null
  );
}

// 获取菜单下拉树列表
export function getMenuTreeSelect() {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/system/menu/treeSelect',
      method: 'get'
    }),
    // 模拟数据
    [
      {
        id: 1,
        label: '系统管理',
        children: [
          {
            id: 11,
            label: '用户管理',
            children: []
          },
          {
            id: 12,
            label: '角色管理',
            children: []
          }
        ]
      },
      {
        id: 2,
        label: '系统监控',
        children: []
      }
    ]
  );
} 